package com.nnxieli.trans.adapter;

import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.hd.trans.ui.base.BaseActivity;
import com.nnxieli.trans.R;
import com.nnxieli.trans.bean.HomeIconBean;

import java.util.ArrayList;

public class HomeAdapter extends RecyclerView.Adapter<HomeAdapter.ViewHolder> {

    private ArrayList<HomeIconBean> list;
    private OnItemClickListener listener;

    public HomeAdapter(ArrayList<HomeIconBean> list) {
        this.list = list;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.home_icon_layout, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        final int mPosition = position;
        HomeIconBean data = list.get(position);
        holder.button.setBackgroundColor(Color.parseColor(data.getBackgroud()));
        holder.image.setImageResource(data.getIcon());
        holder.text.setText(data.getText());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(listener != null){
                    listener.onItemClick(mPosition, data.getText(), data.getMClazz());
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private final RelativeLayout button;
        private final ImageView image;
        private final TextView text;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            button = (RelativeLayout) itemView.findViewById(R.id.home_icon_button);
            image = (ImageView) itemView.findViewById(R.id.home_icon_image);
            text = (TextView) itemView.findViewById(R.id.home_icon_text);
        }
    }

    public void setOnItemClickListener(OnItemClickListener listener){
        this.listener = listener;
    }

    public interface OnItemClickListener {
        void onItemClick(int position, String name, Class<BaseActivity> clazz);
    }
}
